<?php

namespace App\Model;

use App\v1\UserKeepController;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\DB;

class UserModel extends BaseModel
{
    protected $table = 'user';
    public $timestamps = false;
    protected $guarded = [];


//    获取token
    public function getToken($data)
    {
        $openid = $data -> openid;

//        查询openid是否存在
       $isOpenid =  UserModel::where(['openid' =>$openid]) ->first();

       if( !is_null($isOpenid) ){
           return ['code' => true,'data' => ['id' => $isOpenid['id'],'score' => $isOpenid['permission'] ] ];
       }


       $res =  DB::table('user') ->insertGetId([
           'openid' => $openid,
           'permission' => 1,
           'create_at' => date('Y:m:d H:i:s')
       ]);

        if($res > 0){
            return ['code' => true,'data' => ['id' => $res,'score' => 1 ]];
        }else{
            return ['code' => false,'data' => '添加失败！'];
        }

    }

    /**
     * 登陆接口
     * @param $uuid
     * @param $avatarUrl
     * @param $nickName
     * @param $gender
     * @return array
     */
    public function login($uuid,$avatarUrl,$nickName,$gender)
    {

//        查询是否有用户信息
        $res = UserModel::find($uuid);
        $res ->nick_name = $nickName;
        $res ->head_img = $avatarUrl;
        $res ->sex = $gender;
        $res ->update_at = date('Y:m:d H:i:s');
        $res ->save();

       if($res){//成功！
           return ['code' => true,'data' => 1];
       }else{
           return ['code' => false,'data' => '登陆失败！'];
       }


    }

    public function getFocusNum($uuid)
    {
        $res =  UserKeepModel::where(['uid' => $uuid]) ->count();

        if($res){//成功！
            return ['code' => true,'data' => $res];
        }else{
            return ['code' => false,'data' => 0];
        }

    }

    public function getFansNum($uuid)
    {
        $res =  UserKeepModel::where(['focus_id' => $uuid]) ->count();

        if($res){//成功！
            return ['code' => true,'data' => $res];
        }else{
            return ['code' => false,'data' => 0];
        }
    }

    public function getFocusNumTo($uid)
    {
    }


}
